package item.controller;


import commons.pojo.Item;
import commons.pojo.Shipin;
import commons.vo.LivegoodsResult;
import item.pojo.Idd;
import item.pojo.Tager;
import item.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ItemController {
    public static final String PATH_URL = "http://localhost:9015/save";
    @Autowired
    private ItemService itemService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/tag")
    public LivegoodsResult getTag(@RequestBody Tager tag){
        return itemService.getItem(tag.getPage(),10,tag.getS());
    }

    @GetMapping ("/like")
    public Item getLike(@RequestParam int id){
        return itemService.likeItem(id);
    }

    @GetMapping("/likee")
    Shipin likee(@RequestParam int id){return itemService.likeShiPin(id);}

    @RequestMapping ("/likeId")
    public Item getLikeId(@RequestBody Idd iid){
        return itemService.likeItem(iid.getN());
    }
//    @PostMapping("/save")
//    public void save(){
//        List<Item> all = mongoTemplate.findAll(Item.class);
//        List<Article> articles = new ArrayList<>();
//        for (int i = 0;i<=10;i++){
//            Item item = all.get(i);
//            Article article = new Article();
//            article.setTitle(item.getTitle());
//            article.setId(String.valueOf(item.getId()));
//            article.setLike(String.valueOf(item.getLike()));
//            article.setCollection(String.valueOf(item.getCollection()));
//            article.setTag(item.getTag());
//            article.setTxt(item.getTxt().toString());
//            articles.add(article);
//        }

    //
//        JSONObject postData = new JSONObject();
//        postData.put("list",articles);
//        restTemplate.postForObject(PATH_URL, postData, JSONObject.class);
//    }

    @PostMapping("/selectFaShiPage")
    public LivegoodsResult selectFaShiPage(@RequestBody Idd idd) {
        return itemService.selectFaShiPage(idd.getN());
    }

    @PostMapping("/selectFaShi")
    LivegoodsResult selectFaShi(@RequestBody Idd idd) {
        return itemService.selectFaShi(idd.getN());
    }

    @PostMapping("/selectHuaTi")
    LivegoodsResult selectHuaTi() {
        return itemService.selectHuaTi();
    }

    @PostMapping("/selectHuaTiId")
    LivegoodsResult selectHuaTiId(@RequestBody Idd idd) {
        return itemService.selectHuaTiId(idd.getN());
    }

    @PostMapping("selectJingXing")
    LivegoodsResult selectJingXing() {
        return itemService.selectJingXing();
    }

    @PostMapping("/selectJingXingId")
    LivegoodsResult selectJingXingId(@RequestBody Idd idd) {
        return itemService.selectJingXingId(idd.getN());
    }




    @PostMapping("/likeAns")
    LivegoodsResult likeAns(@RequestBody Idd idd){return LivegoodsResult.ok(itemService.likeShiPin(idd.getN()));}

    @PostMapping("selectShiPinPage")
    LivegoodsResult selectShiPinPage(@RequestBody Idd idd){return itemService.selectShiPinPage(idd.getN());}


}